using System;
using System.Collections;
using System.Windows.Forms;

namespace ctsu.Admin.Forms
{
	/// <summary>
	/// Summary description for FormOptions.
	/// </summary>
	public class FormOptions : System.Windows.Forms.Form
	{
		public delegate void OptionsChangedHandler(OptionsChangedArgs oca);
		public static event OptionsChangedHandler OnOptionsChanged;

		private Hashtable _configData;
		private Hashtable _htLoadedPages;
		private Hashtable _htChangedPages;

		private System.Windows.Forms.TabPage tabPage2;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TabControl tab;
		private System.Windows.Forms.CheckBox CloseOnCancel;
		private System.Windows.Forms.CheckBox CloseOnSave;
		private System.Windows.Forms.CheckBox SummaryValidate;
		private System.Windows.Forms.CheckBox FieldValidate;
		private System.Windows.Forms.TabPage FormsConfig;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnApply;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FormOptions(Hashtable ht)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			_configData = ht;

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormOptions));
			this.tab = new System.Windows.Forms.TabControl();
			this.FormsConfig = new System.Windows.Forms.TabPage();
			this.CloseOnCancel = new System.Windows.Forms.CheckBox();
			this.CloseOnSave = new System.Windows.Forms.CheckBox();
			this.label2 = new System.Windows.Forms.Label();
			this.SummaryValidate = new System.Windows.Forms.CheckBox();
			this.FieldValidate = new System.Windows.Forms.CheckBox();
			this.label1 = new System.Windows.Forms.Label();
			this.tabPage2 = new System.Windows.Forms.TabPage();
			this.btnOK = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnApply = new System.Windows.Forms.Button();
			this.tab.SuspendLayout();
			this.FormsConfig.SuspendLayout();
			this.SuspendLayout();
			// 
			// tab
			// 
			this.tab.Controls.Add(this.FormsConfig);
			this.tab.Controls.Add(this.tabPage2);
			this.tab.Location = new System.Drawing.Point(8, 8);
			this.tab.Name = "tab";
			this.tab.SelectedIndex = 0;
			this.tab.Size = new System.Drawing.Size(472, 240);
			this.tab.TabIndex = 0;
			this.tab.SelectedIndexChanged += new System.EventHandler(this.tab_SelectedIndexChanged);
			// 
			// FormsConfig
			// 
			this.FormsConfig.Controls.Add(this.CloseOnCancel);
			this.FormsConfig.Controls.Add(this.CloseOnSave);
			this.FormsConfig.Controls.Add(this.label2);
			this.FormsConfig.Controls.Add(this.SummaryValidate);
			this.FormsConfig.Controls.Add(this.FieldValidate);
			this.FormsConfig.Controls.Add(this.label1);
			this.FormsConfig.Location = new System.Drawing.Point(4, 22);
			this.FormsConfig.Name = "FormsConfig";
			this.FormsConfig.Size = new System.Drawing.Size(464, 214);
			this.FormsConfig.TabIndex = 0;
			this.FormsConfig.Text = "Forms";
			// 
			// CloseOnCancel
			// 
			this.CloseOnCancel.Location = new System.Drawing.Point(192, 32);
			this.CloseOnCancel.Name = "CloseOnCancel";
			this.CloseOnCancel.Size = new System.Drawing.Size(128, 16);
			this.CloseOnCancel.TabIndex = 17;
			this.CloseOnCancel.Text = "Close On Cancel";
			// 
			// CloseOnSave
			// 
			this.CloseOnSave.Location = new System.Drawing.Point(72, 32);
			this.CloseOnSave.Name = "CloseOnSave";
			this.CloseOnSave.Size = new System.Drawing.Size(112, 16);
			this.CloseOnSave.TabIndex = 16;
			this.CloseOnSave.Text = "Close On Save";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(48, 16);
			this.label2.TabIndex = 15;
			this.label2.Text = "Closing:";
			// 
			// SummaryValidate
			// 
			this.SummaryValidate.Location = new System.Drawing.Point(136, 8);
			this.SummaryValidate.Name = "SummaryValidate";
			this.SummaryValidate.Size = new System.Drawing.Size(80, 16);
			this.SummaryValidate.TabIndex = 14;
			this.SummaryValidate.Text = "Summary";
			// 
			// FieldValidate
			// 
			this.FieldValidate.Location = new System.Drawing.Point(72, 8);
			this.FieldValidate.Name = "FieldValidate";
			this.FieldValidate.Size = new System.Drawing.Size(64, 16);
			this.FieldValidate.TabIndex = 13;
			this.FieldValidate.Text = "Field";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(64, 16);
			this.label1.TabIndex = 12;
			this.label1.Text = "Validation:";
			// 
			// tabPage2
			// 
			this.tabPage2.Location = new System.Drawing.Point(4, 22);
			this.tabPage2.Name = "tabPage2";
			this.tabPage2.Size = new System.Drawing.Size(464, 214);
			this.tabPage2.TabIndex = 1;
			this.tabPage2.Text = "tabPage2";
			// 
			// btnOK
			// 
			this.btnOK.Enabled = false;
			this.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnOK.Image = ((System.Drawing.Image)(resources.GetObject("btnOK.Image")));
			this.btnOK.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnOK.Location = new System.Drawing.Point(152, 256);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(80, 23);
			this.btnOK.TabIndex = 1;
			this.btnOK.Text = "&OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnCancel.Image = ((System.Drawing.Image)(resources.GetObject("btnCancel.Image")));
			this.btnCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnCancel.Location = new System.Drawing.Point(251, 256);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(85, 23);
			this.btnCancel.TabIndex = 2;
			this.btnCancel.Text = "&Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnApply
			// 
			this.btnApply.Enabled = false;
			this.btnApply.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
			this.btnApply.Image = ((System.Drawing.Image)(resources.GetObject("btnApply.Image")));
			this.btnApply.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btnApply.Location = new System.Drawing.Point(352, 256);
			this.btnApply.Name = "btnApply";
			this.btnApply.Size = new System.Drawing.Size(80, 23);
			this.btnApply.TabIndex = 3;
			this.btnApply.Text = "&Apply";
			this.btnApply.Click += new System.EventHandler(this.btnApply_Click);
			// 
			// FormOptions
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(488, 285);
			this.Controls.Add(this.btnApply);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.tab);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "FormOptions";
			this.Text = "Options";
			this.Load += new System.EventHandler(this.FormOptions_Load);
			this.tab.ResumeLayout(false);
			this.FormsConfig.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void tab_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Console.WriteLine("tab_SelectedIndexChanged: " + tab.SelectedIndex.ToString());
			//TODO need to check if it's already loaded

			LoadOptions(tab.SelectedTab.Name);

		}

		private void FormOptions_Load(object sender, System.EventArgs e)
		{
			this._htChangedPages = new Hashtable();
			//this is the first tab			
			_htLoadedPages = new Hashtable();

			_htLoadedPages.Add("FormsConfig", (Hashtable)this._configData["FormsConfig"]);
			LoadOptions("FormsConfig");
		}

		private void LoadOptions(string tabName)
		{

			//get the hashTable from the configData
			if (!this._htLoadedPages.Contains(tabName))
				return;

			Hashtable ht = (Hashtable)this._htLoadedPages[tabName];
			foreach (Control ctl in tab.TabPages[tab.SelectedIndex].Controls)
			{
				if (ht.Contains(ctl.Name))
				{

					string sVal = (string)ht[ctl.Name];
					string ctlType = ctl.GetType().ToString();
					switch (ctlType)
					{
						case "System.Windows.Forms.TextBox":
							ctl.Text = sVal;
							ctl.TextChanged += new System.EventHandler(this.Central_Changed);
							break;
						case "System.Windows.Forms.CheckBox":
							CheckBox cb = (CheckBox)ctl;
							cb.Checked = bool.Parse(sVal);
							cb.CheckedChanged += new System.EventHandler(this.Central_Changed);
							break;
					}
				}
			}
		}

		private void Central_Changed(object sender, System.EventArgs e)
		{
			btnOK.Enabled = true;
			btnApply.Enabled = true;

			Control ctl = (Control)sender;
			TabPage tb = (TabPage)ctl.Parent;
			string tabName = tb.Name;
			ctl.Tag = true;

			if (!_htChangedPages.ContainsKey(tabName))
				_htChangedPages.Add(tabName, tb);

			Console.WriteLine("Central_TextChanged - TabName: " + tb.Name);
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			SaveOptions();
			this.Close();
		}

		private void SaveOptions()
		{
			foreach (DictionaryEntry e in this._htChangedPages)
			{
				SaveTab((TabPage)e.Value);
			}
			this._htChangedPages.Clear();
		}

		private void SaveTab(TabPage tb)
		{

			//config hash
			Hashtable ht = (Hashtable)this._htLoadedPages[tb.Name];
			foreach (Control ctl in tb.Controls)
			{
				if (ht.Contains(ctl.Name))
				{
					if (ctl.Tag != null)
					{
						string ctlType = ctl.GetType().ToString();
						switch (ctlType)
						{
							case "System.Windows.Forms.TextBox":
								ht[ctl.Name] = ctl.Text;
								break;
							case "System.Windows.Forms.CheckBox":
								CheckBox cb = (CheckBox)ctl;
								object old = ht[ctl.Name];
								ht[ctl.Name] = cb.Checked.ToString();
								object newo = ht[ctl.Name];

								OptionsChangedArgs oca = new OptionsChangedArgs(ctl.Name, old, newo);
								if (OnOptionsChanged != null)
									OnOptionsChanged(oca);

								break;
						}
					}
				}
			}

		}

		private void btnApply_Click(object sender, System.EventArgs e)
		{
			SaveOptions();

			btnOK.Enabled = false;
			btnApply.Enabled = false;
		}
	}

	public class OptionsChangedArgs
	{
		public string Option;
		public object OldValue;
		public object NewValue;

		public OptionsChangedArgs(string option, object oldValue, object newValue)
		{
			this.Option = option;
			this.OldValue = oldValue;
			this.NewValue = newValue;
		}
	}

}
